package com.web.dao;

import com.web.model.user;
import com.web.util.CommonDao;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class UserDao {

    private JdbcTemplate jdbcTemplate;
    private CommonDao commondao;
    public UserDao(JdbcTemplate jdbcTemplate) {
        super();
        this.jdbcTemplate = jdbcTemplate;
        if(this.jdbcTemplate !=null)
            this.commondao = new CommonDao(this.jdbcTemplate);
    }

    public user selectByUsername(String username) throws Exception {
        StringBuilder sql = new StringBuilder();
        List<Object> paramList = new ArrayList<Object>();
        sql.append(" select * from userinfo where username = '"+username+"'");
        System.out.println(sql);
        user user =new user();
        List<Map<String, Object>> userList = this.commondao.query(sql.toString(), paramList);
        Map<String, Object> userMap = userList.get(0);
        user.setId(userMap.get("id").toString());
        user.setUsername(userMap.get("username").toString());
        //user.setUserGroup( userMap.get("usergroup"));
        user.setUserAuthority((Integer) userMap.get("userAuthority"));
        user.setPassword(userMap.get("password").toString());
        user.setEmployeeNo(userMap.get("EmployeeNo").toString());
        user.setRole(userMap.get("role").toString());
        return user;
    }
}
